﻿using BCCommon;
using BCEntity.TMS.Agent.AgentCurrentlyProtocolAttachment;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;

namespace BCData.TMS.Agent.AgentCurrentlyProtocolAttachment
{
    public class AgentCurrentlyProtocolAttachmentData : IAgentCurrentlyProtocolAttachmentData
    {
        private readonly IDatabaseContext database;

        private const string QUERY = @"SELECT AgentCurrentlyProtocolAttachmentId,AgentCurrentlyProtocolId,AttachmentAddress,AttachmentOrder,CreateTime 
                                    FROM AgentCurrentlyProtocolAttachment";

        public AgentCurrentlyProtocolAttachmentData(IDatabaseContext database)
        {
            this.database = database;
        }

        public async Task<int> InsertAll(List<AgentCurrentlyProtocolAttachmentEntity> entities)
        {
            return await database.Master.InsertAllAsync(entities, database.Transaction);
        }

        public async Task<int> Delete(long agentCurrentlyProtocolId)
        {
            return await database.Master.ExecuteAsync("DELETE FROM AgentCurrentlyProtocolAttachment WHERE AgentCurrentlyProtocolId = ?AgentCurrentlyProtocolId", new { agentCurrentlyProtocolId }, database.Transaction);
        }

        public async Task<IEnumerable<AgentCurrentlyProtocolAttachmentEntity>> GetListByIds(List<long> agentCurrentlyProtocolIds)
        {
            return await database.Slave.QueryAsync<AgentCurrentlyProtocolAttachmentEntity>($"{QUERY} WHERE FIND_IN_SET(AgentCurrentlyProtocolId,?AgentCurrentlyProtocolIds)", new { AgentCurrentlyProtocolIds = string.Join(",", agentCurrentlyProtocolIds) });
        }

        public async Task<IEnumerable<AgentCurrentlyProtocolAttachmentEntity>> GetList(long agentCurrentlyProtocolId)
        {
            return await database.Slave.QueryAsync<AgentCurrentlyProtocolAttachmentEntity>($"{QUERY} WHERE AgentCurrentlyProtocolId = ?AgentCurrentlyProtocolId", new { agentCurrentlyProtocolId });
        }
    }
}
